package top.littlejiang.onlinexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.base.BasePage;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamSysLog;
import top.littlejiang.onlinexam.entity.OlexamUser;
import top.littlejiang.onlinexam.service.OlexamSysLogService;
import top.littlejiang.onlinexam.service.OlexamSysService;
import top.littlejiang.onlinexam.service.OlexamUserCourseService;
import top.littlejiang.onlinexam.service.OlexamUserService;

import java.io.Serializable;
import java.util.List;

/**
 * @author chen
 * @Description
 * @since 2021/2/28 13:59
 */
@Controller
@RequestMapping("/sys")
public class SysController {

    @Autowired
    OlexamUserService userService;
    @Autowired
    OlexamSysLogService sysLogService;
    @Autowired
    OlexamSysService sysService;
    @Autowired
    OlexamUserCourseService userCourseService;

    /**
     * 分页查询列表
     * @param page
     * @return
     */
    @PostMapping("/userlist")
    @ResponseBody
    @RequiresPermissions("olexam:sys:user")
    public Result<IPage<OlexamUser>> getUserlist(@RequestBody BasePage<OlexamUser> page){
        IPage<OlexamUser> userListByName = userService.getUserListPage(page);
        return Result.ok(userListByName);
    }

    /**
     * 批量禁用用户
     * @param ids
     * @return
     */
    @PostMapping("deluser")
    @ResponseBody
    @RequiresPermissions("olexam:sys:user")
    @Log("禁用用户")
    public Result delUserList(@RequestBody List<Serializable> ids){
        if (CollectionUtils.isEmpty(ids)){
            return Result.fail();
        }
        userService.logicDeleteById(ids);
        userCourseService.deleteByUser(ids);
        return Result.ok();
    }

    /**
     * 新增一个用户
     * @param user
     * @return
     */
    @PostMapping("saveoneuser")
    @ResponseBody
    @RequiresPermissions("olexam:sys:user")
    @Log("新增用户")
    public Result saveOneUser(@RequestBody OlexamUser user){
        sysService.saveOneUser(user);
        return Result.ok();
    }

    /**
     * 更新用户
     * @param user
     * @param file
     * @return
     */
    @PostMapping("/updateUser")
    @ResponseBody
    @RequiresPermissions("olexam:sys:user")
    @Log("更新用户")
    public Result update(OlexamUser user, MultipartFile file){
        sysService.updateUser(user, file);
        return Result.ok();
    }

    /**
     * 系统日志列表分页显示
     * @param page
     * @return
     */
    @PostMapping("/loglist")
    @ResponseBody
    public Result logList(@RequestBody BasePage<OlexamSysLog> page) {
        return Result.ok(sysLogService.getSyslogListPage(page));
    }

    /**
     * 系统日志 物理删除
     * @param ids
     * @return
     */
    @PostMapping("/delsyslog")
    @ResponseBody
    @RequiresPermissions("olexam:sys:log")
    @Log("删除系统日志")
    public Result delete(@RequestBody List<Serializable> ids){
        sysLogService.deleteById(ids);
        return Result.ok();
    }

    /**
     * 获取系统时间 毫秒 timestamp
     * @return
     */
    @PostMapping("/timestamp")
    @ResponseBody
    public Result getTimeStamp(){
        return Result.ok(System.currentTimeMillis());
    }

}
